package com.zoweunion.mechanic.controller;

import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.entity.TSSsWarehouselocationEntity;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.ITSSsWarehouselocationService;
import com.zoweunion.mechanic.service.impl.TSSsWarehouseRegionServiceImpl;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author wxx
 * @date 2023-04-20
 */
@RestController
@RequestMapping(path = "/tSSsWarehouselocation", produces = "application/json;charset=UTF-8")
// @Api(value = "/tSSsWarehouselocation", description = "${comments}", produces = "application/json;charset=UTF-8")

public class TSSsWarehouselocationController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(TSSsWarehouselocationController.class);

    @Autowired
    private ITSSsWarehouselocationService tSSsWarehouselocationService;

    @Autowired
    private TSSsWarehouseRegionServiceImpl warehouseRegionService;

    @GetMapping("/listPage")
//     @ApiOperation(value = "条件查询列表分页", notes = "条件查询列表分页")
    public ResponseBean listPage(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {

        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = tSSsWarehouselocationService.listPage(params);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }


    /**
     * 新增
     */
    @PostMapping
//    @ApiOperation(value = "新增", notes = "")
    public ResponseBean create(@RequestBody @Validated TSSsWarehouselocationEntity tSSsWarehouselocation, HttpServletRequest request) {

        try {
            tSSsWarehouselocationService.save(tSSsWarehouselocation);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            return new ResponseBean(Constants.CODE_ERROR, e.getMessage(), "");
        }


    }


    /**
     * 更新
     */
    @PutMapping
//    @ApiOperation(value = "更新", notes = "")
    public ResponseBean update(@RequestBody TSSsWarehouselocationEntity tSSsWarehouselocation, HttpServletRequest request) throws MyException {

        if (tSSsWarehouselocation.getId() != null) {

            // 删除原有的
            Map<String,Object> deleteMap = new HashMap<>();
            deleteMap.put("warehouse_id",tSSsWarehouselocation.getId());
            warehouseRegionService.removeByMap(deleteMap);
            // 添加管辖范围
            Object provinceListObj = tSSsWarehouselocation.getProvinceList();
            if (provinceListObj != null) {
                warehouseRegionService.addWarehouseRegion((List<Map<String, Object>>) provinceListObj, tSSsWarehouselocation.getId(), getCurrentUser(request).getS_id(), getCurrentUser(request).getId());
            }

            tSSsWarehouselocationService.updateById(tSSsWarehouselocation);

            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } else {
            return new ResponseBean(Constants.CODE_ERROR, "id必填", "");
        }
    }


    /**
     * 详情
     */
    @GetMapping("/{id}")
//    @ApiOperation(value = "详情", notes = "")
    public ResponseBean detail(@PathVariable("id") String id) {
        try {

            TSSsWarehouselocationEntity tSSsWarehouselocation =  tSSsWarehouselocationService.detail(id);

            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", tSSsWarehouselocation);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作失败", "");
        }

    }


    /**
     * 删除
     */
    @DeleteMapping
//    @ApiOperation(value = "删除", notes = "")
    public ResponseBean delete(@RequestBody String[] ids) {
        try {

            tSSsWarehouselocationService.removeByIds(Arrays.asList(ids));
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作成功", "");
        }
    }

//    /**
//     * 列表导出
//     */
//    @GetMapping("/listExport")
//    public void listExport(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
//        List<TSSsWarehouselocationExcel> info = tSSsWarehouselocationService.listExport(params);
//        ExcelUtils.exportExcelToTarget(response, "列表导出", info, TSSsWarehouselocationExcel.class);
//    }


}
